﻿@using DeliveryPlus.Web.Classes.Enums
@using DeliveryPlus.Web.DAL
@model DeliveryPlus.Web.DAL.Entities.Dishe

@{
    ViewBag.Title = Model.Id <= 0 ? "Создание блюда" : "Редактирование блюда " + Model.Title;
    Layout = "../Shared/ControlPanelLayout.cshtml";
    var dc = new DeliveryPlusDataContext();
    var cafes = dc.Cafes.OrderBy(c => c.Title).ToList();
    var types = (Model.Types ?? "").Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(c => Convert.ToInt16(c)).Select(c => (DishTypes)c).ToArray();
    var dishTypes = dc.DishTypes.OrderBy(t => t.Title).ToList();
    int selectedKitchenType = -1;
}

<form role="form" id="dish-form" action="/ControlPanel/ManageDishes/Save" method="POST" enctype="multipart/form-data">
    @Html.HiddenFor(m => m.Id)
    <div class="form-group">
        <label for="dish-title-field">Название</label>
        <input type="text" class="form-control" name="Title" id="dish-title-field" placeholder="Введите название" value="@Model.Title">
    </div>
    <div class="form-group">
        <label for="dish-cafe-field">Кафе</label>
        <select id="dish-cafe-field" class="form-control" name="CafeId">
            @foreach (var cafe in cafes)
            {
                <option value="@cafe.Id" @(Model.CafeId == cafe.Id ? "selected=selected" : "")>@cafe.Title</option>
            }
        </select>
    </div>
    <div class="form-group">
        <label for="dish-type-field">Кухня</label>
        <select id="dish-kitchen-field" class="form-control" name="TypeId" >
            <option value="-1">Не указан</option>
            @foreach (var group in dishTypes.GroupBy(g => g.GroupName))
            {
                <option value="@group.Key">@group.Key</option>
            }
        </select>
    </div>

    <div class="form-group">
        <label for="dish-type-field">Тип блюда</label>
        <select id="dish-type-field" class="form-control" name="TypeId">
            <option value="-1">Не указан</option>
            @foreach (var group in dishTypes.GroupBy(g => g.GroupName))
            {
                    foreach (var type in group)
                    {
                        <option value="@type.Id" @(Model.TypeId == type.Id ? "selected=selected" : "")>@type.Title</option>
                    }        
            }
        </select>
    </div>

    <div class="form-group">
        <label for="dish-desc-field">Описание</label>
        <textarea class="form-control" name="Description" placeholder="Введите описание блюда" id="dish-desc-field">@Model.Description</textarea>
    </div>
    <div class="form-group">
        <label for="dish-en-field">Артикул (внутренний номер)</label>
        <textarea class="form-control" name="Energy" placeholder="Введите описание артикула, внутреннего номера блюда" id="dish-en-field">@Model.Energy</textarea>
    </div>
    <div class="form-group">
        <label for="dish-title-field">Вес</label>
        <input type="text" class="form-control" name="Weight" id="dish-weight-field" placeholder="Введите вес блюда" value="@Model.Weight">
    </div>
    <div class="form-group">
        <label for="">Не отображать в списке</label>
        @Html.CheckBoxFor(m => m.Hidden)
    </div>
    <div class="form-group">
        <label for="dish-price-field">Цена</label>
        <input type="text" class="form-control" name="Price" id="dish-price-field" placeholder="Введите цену" value="@Model.Price">
    </div>
    @*
    @foreach (DishTypes type in Enum.GetValues(typeof(DishTypes)))
    {
        var t = (short) type;
        if (t != 0)
        {
        <div class="form-group">
            <label for="dish-price-field">@type</label>
            <input type="checkbox" class="form-control" name="types-@t" value="@t" @(types.Contains(type) ? "checked=checked":"")/>
        </div>
        }
    }*@
    <div class="form-group">
        <label for="dish-photo-field">Фотография</label>
        <input type="file" id="dish-photo-field" name="photoFile">
        <p class="help-block">Выберайте только качественную фотографию или логотип</p>
    </div>
    <input type="hidden" id="next-field" name="next" value="" />
    <button type="submit" class="btn btn-default">Сохранить</button>
    <button type="submit" class="btn btn-success" onclick="$('#next-field').val('next');">Сохранить и создать следующее блюдо</button>
</form>
<script type="text/javascript">
    $(document).ready(function () {
        $("#dish-form").validate({
            rules: {
                Title: {
                    required: true,
                    maxlength: 255
                },
                Description: {
                    required: false,
                    maxlength: 1000
                },
                Energy: {
                    required: false,
                    maxlength: 1000
                },
                Weight: {
                    required: false,
                    maxlength: 1000
                },
                Price: {
                    required: true
                },
            }
        });
    });
</script>
